package com.hengtiansoft.lfy.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.hengtiansoft.lfy.LFYApplication;
import com.hengtiansoft.lfy.chart.DemoHXSDKHelper;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.UserInfo;
import com.hyphenate.EMCallBack;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * SharedPreferences配置信息
 * 
 */
public class Config {

	// 用户信息
	private static String USER_INFO = "USER_INFO";

	// 第一次启动
	private static String FIRST_START = "FIRST_START";
	private static String DOWN_LOAD = "DOWN_LOAD";
	public static final String PREFERENCE_NAME = "lfy";
	private static String AUTOPLAYER = "auto_player";
	public static SharedPreferences UserInfoPreferences = LFYApplication.getContext().getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
	public static SharedPreferences sharedPreferencesInterPreferences =  LFYApplication.getContext().getSharedPreferences(
			Constants.INTERPREFERENCES, Context.MODE_PRIVATE);
	public static SharedPreferences sharedPreferencesChatTranslationKinds=LFYApplication.getContext().getSharedPreferences(
			Constants.CHATTRANSLATIONKINDS, Context.MODE_PRIVATE);
	public static SharedPreferences mSharedPreferences=LFYApplication.getContext().getSharedPreferences(
			Constants.CHATTRANSLATIONKINDS, Context.MODE_PRIVATE);
	public static boolean isFirst() {
		return UserInfoPreferences.getBoolean(FIRST_START, true);
	}

	public static void setFirst(boolean isFirst) {
		UserInfoPreferences.edit().putBoolean(FIRST_START, isFirst).commit();
	}
	public static void setDownLoad(String timer) {
		UserInfoPreferences.edit().putString(DOWN_LOAD, timer).commit();
	}
	public static String getDownLoad() {
		return UserInfoPreferences.getString(DOWN_LOAD,"");
	}
	/**
	 * 清空数据
	 */
	public static void cleanData() {
		UserInfoPreferences.edit().clear();
	}
	
	public static int width = 0;
	public static int height = 0;
	public static float density;

	public static int getScreenWidth() {
		return width;
	}

	public static int getScreenHeight() {
		return height;
	}

	/**
	 * 得到屏幕长宽
	 * @param activity
	 */
	public static void setScreenSize(Activity activity) {
		DisplayMetrics displayMetrics = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);
		width = displayMetrics.widthPixels;
		height = displayMetrics.heightPixels;
		density = displayMetrics.density;
	}

	/**UserInfo*/
	private static String userinfo = "userinfo";
	public static UserInfo mUserInfo  = new UserInfo();

	public static void setUserInfo(UserInfo user) {
		try {
			String userInfoStr =  serialize(user);
			UserInfoPreferences.edit().putString(userinfo, userInfoStr).commit();
		}catch(Exception e){
			e.printStackTrace();
		}
		mUserInfo = user;
	}

//	public static UserInfo getUserInfo() {
//		if( UserInfoPreferences.contains(userinfo)){
//			try {
//				String userInfoStr = UserInfoPreferences.getString(userinfo, "");
//				mUserInfo =  deSerialize(userInfoStr);
//			}catch(Exception e){
//				e.printStackTrace();
//			}
//		}
//
//		return mUserInfo ;
//	}

	/**
	 * 序列化对象
	 *
	 * @return
	 * @throws IOException
	 */
	public static String serialize(UserInfo user) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		objectOutputStream.writeObject(user);
		String serStr = byteArrayOutputStream.toString("ISO-8859-1");
		serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
		objectOutputStream.close();
		byteArrayOutputStream.close();
		return serStr;
	}

	/**
	 * 反序列化对象
	 *
	 * @param str
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static UserInfo deSerialize(String str) throws IOException, ClassNotFoundException {
		String redStr = java.net.URLDecoder.decode(str, "UTF-8");
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				redStr.getBytes("ISO-8859-1"));
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		UserInfo person = (UserInfo) objectInputStream.readObject();
		objectInputStream.close();
		byteArrayInputStream.close();
		return person;
	}

	public static final String TOKEN = "token";

	/**
	 * 保存登陆用户token
	 * @param
	 */
	public static void setUserToken(String token) {
		UserInfoPreferences.edit().putString(TOKEN, token).commit();
	}

	/**
	 * 获取登陆用户token
	 * @param
	 */
	public static String getUserToken() {
		String token = UserInfoPreferences.getString(TOKEN, "");
		return token;
	}

	/**
	 * 用户是否登陆
	 * @param
	 */
	public static boolean isUserLogined() {
//		boolean isLogin = false;
//		UserInfo userInfo = Config.getUserInfo();
//		if (userInfo.getUsername() != null && userInfo.getUsername().length() > 0) {
//			isLogin = true;
//		}
		return DemoHXSDKHelper.getInstance().isLogined() ;
	}

	/**
	 * 获取登陆用户名
	 * @param
	 */
	public static String getUserName(){
		return LFYApplication.hxSDKHelper.getHXId();
	}
	/**
	 * 退出登录,清空数据
	 */
	public void logout(final boolean isGCM, final EMCallBack emCallBack) {
		// 先调用sdk logout，在清理app中自己的数据
		LFYApplication.hxSDKHelper.logout(isGCM, emCallBack);
	}
	/**
	 * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
	 * 内部的自动登录需要的密码，已经加密存储了
	 *
	 * @param pwd
	 */
	public static void setPassword(String pwd) {
		LFYApplication.hxSDKHelper.setPassword(pwd);
	}
	/**
	 * 设置用户名
	 *
	 * @param
	 */
	public static void setUserName(String username) {
		LFYApplication.hxSDKHelper.setHXId(username);
	}


	/**
	 * 获取密码
	 *
	 * @return
	 */
	public static String getPassword() {
		return  LFYApplication.hxSDKHelper.getPassword();
	}
//	/**
//	 * 注销
//	 */
//	public static void loggedOut(){
//		Config.setUserInfo(new UserInfo());
//	}

	public static void writeInterPreferencesPreference(int position) {
		SharedPreferences.Editor editor1 = sharedPreferencesInterPreferences
				.edit();
		editor1.putInt("position", position);
		// 提交数据
		editor1.commit();
	}

	public static int getInterPreferencesposition() {
		return sharedPreferencesInterPreferences.getInt("position", 0);
	}


	public static int getposition() {
		return sharedPreferencesChatTranslationKinds.getInt("position", 0);
	}
	public static boolean getSettingAutoPlayer() {
		return mSharedPreferences.getBoolean(AUTOPLAYER, true);
	}
}
